using System;
using System.IO;
using System.Net;
using System.Text;
using SmartBoard.Core.Networking;

namespace SmartBoard.Server
{
    public class SmartBoardServer
    {
        private SocketServer _chatServer;

        public static ITextWriter Out { get; private set; }
        public static ITextWriter Error { get; private set; }

        public SmartBoardServer(ITextWriter infoStream, ITextWriter errorStream)
        {
            Out = infoStream;
            Error = errorStream;
        }

        public void Start()
        {
            _chatServer = new SocketServer(CallbackThreadType.WorkerThread,
                                    new SmartBoardConnectionService(),
                                    DelimiterType.MessageTailExcludeOnReceive,
                                    new byte[] { 0xAA, 0xFF, 0xAA });

            _chatServer.AddListener("Chat Server", new IPEndPoint(IPAddress.Any, 8090),
                EncryptionType.None,
                CompressionType.None,
                new ChatCryptService.ChatCryptService(),
                50, 2);

            _chatServer.Start();

            Out.WriteLine(" Chat Server Started!");
            Out.WriteLine("--------------------------------------");
        }

        public void Stop()
        {
            Out.WriteLine(" Chat Server Stopping!");
            Out.WriteLine("--------------------------------------");

            _chatServer.Stop();
            _chatServer.Dispose();
            Out.WriteLine(" Chat Server Stopped");
            Out.WriteLine("--------------------------------------");
        }
    }
}